package com.qf.context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
杨威
2021/9/13
19:41
*/
@WebServlet("/count2")
public class CountServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        ServletContext application = req.getServletContext();
        Integer count = (Integer)application.getAttribute("count");
        if(count==null){
            count=1;
        }else {
            count++;
        }
        application.setAttribute("count",count);
        PrintWriter writer = resp.getWriter();
        writer.write("您是第"+count+"位访问本网站的客户");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
